package ace.cmp.spring.graphql.data.jdbc.querydsl.autoconfig;

import ace.cmp.spring.graphql.core.execution.AceBatchLoaderRegistry;
import ace.cmp.spring.graphql.data.jdbc.querydsl.core.configurer.*;
import ace.cmp.spring.graphql.data.jdbc.querydsl.core.controller.converter.PageInputToPageableConverter;
import ace.cmp.spring.graphql.data.jdbc.querydsl.core.controller.converter.impl.PageInputToPageableConverterImpl;
import ace.cmp.spring.graphql.data.jdbc.querydsl.core.dataloader.GraphqlControllerDataLoaderRegister;
import ace.cmp.spring.graphql.data.jdbc.querydsl.core.dataloader.impl.GraphqlControllerDataLoaderRegisterImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.graphql.GraphQlSourceBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author caspar
 * @date 2023/8/24 13:54 1、注册分页结果类型graphql schema定义
 */
@Configuration
public class GraphqlJdbcAutoConfig {
  @Bean
  public GraphQlSourceBuilderCustomizer graphQlSourceBuilderInjectPageTypeDefinitionCustomizer() {
    GraphQlSourceBuilderCustomizer customizer =
        builder -> {
          builder.configureTypeDefinitions(new GraphqlPageTypeDefinitionConfigurer());
        };
    return customizer;
  }

  @ConditionalOnMissingBean
  @Bean
  public PageInputToPageableConverter pageInputToPageableConverter() {
    return new PageInputToPageableConverterImpl();
  }

  @ConditionalOnMissingBean
  @Bean
  public GraphqlControllerDataLoaderRegister graphqlControllerDataLoaderRegister(
      AceBatchLoaderRegistry loaderRegistry) {
    return new GraphqlControllerDataLoaderRegisterImpl(loaderRegistry);
  }
}
